package les.projeto;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.ToggleButton;

public class EditClassView extends Activity{
    private String subjectName = "";
    private String subjectTeacher = "";
    private String subjectPlace = "";
    private int subjectCredits = 1;
    private String initialDate = "";
    private String endDate = "";
	public Calendar date;
	public int today;
	private int id;
	private String initialTime = "";
    private String repetitions = "";
    private String endTime = "";
	
	public void onCreate(Bundle savedInstanceState) {
	    
	    super.onCreate(savedInstanceState);
	    Intent intent = getIntent();
	    initialDate = getIntent().getStringExtra("initialDate");
        endDate = getIntent().getStringExtra("endDate");
	    subjectName = intent.getStringExtra("subjectName");
        subjectTeacher = intent.getStringExtra("subjectTeacher");
        subjectPlace = intent.getStringExtra("subjectPlace");
        subjectCredits = Integer.valueOf(intent.getStringExtra("subjectCredits"));
	    setContentView(R.layout.editclass);
	    date = Calendar.getInstance();
	    onNewIntent(getIntent());	    
	
	    getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
	
	    id = Integer.valueOf(intent.getStringExtra("id"));
	    initialTime = intent.getStringExtra("initialTime");
        endTime = intent.getStringExtra("endTime");
        repetitions = intent.getStringExtra("repetitions");
	    
	    final TimePicker timePicker = (TimePicker)findViewById(R.id.timePicker1);
	    timePicker.setIs24HourView(true);
	    String[] initial = initialTime.split(":");
	    timePicker.setCurrentHour(Integer.valueOf(initial[0]));
	    timePicker.setCurrentMinute(Integer.valueOf(initial[1]));
	    
	    
	    final TimePicker timePicker2 = (TimePicker)findViewById(R.id.TimePicker02);
        timePicker2.setIs24HourView(true);
        String[] end = endTime.split(":");
        timePicker2.setCurrentHour(Integer.valueOf(end[0]));
        timePicker2.setCurrentMinute(Integer.valueOf(end[1]));
        
        final ToggleButton dom = (ToggleButton)findViewById(R.id.ToggleButton01);
        if (repetitions.contains("1"))
            dom.setChecked(true);
        final ToggleButton seg = (ToggleButton)findViewById(R.id.ToggleButton02);
        if (repetitions.contains("2"))
            seg.setChecked(true);
        final ToggleButton ter = (ToggleButton)findViewById(R.id.ToggleButton03);
        if (repetitions.contains("3"))
            ter.setChecked(true);
        final ToggleButton qua = (ToggleButton)findViewById(R.id.ToggleButton04);
        if (repetitions.contains("4"))
            qua.setChecked(true);
        final ToggleButton qui = (ToggleButton)findViewById(R.id.ToggleButton05);
        if (repetitions.contains("5"))
            qui.setChecked(true);
        final ToggleButton sex = (ToggleButton)findViewById(R.id.ToggleButton06);
        if (repetitions.contains("6"))
            sex.setChecked(true);
        final ToggleButton sab = (ToggleButton)findViewById(R.id.ToggleButton07);
        if (repetitions.contains("7"))
            sab.setChecked(true);
        
       
	  
	    
	    
	    Button save = (Button)findViewById(R.id.buttonSave);
	    save.setOnClickListener(new OnClickListener() {
	        
			
			@Override
			public void onClick(View v) {
			    String repeticoes = "";
			    if (dom.isChecked()){
			        repeticoes +="-" + Calendar.SUNDAY;
			    }
			    if (seg.isChecked()){
			        repeticoes+= "-" + Calendar.MONDAY;
			    }
			    if (ter.isChecked()){
                    repeticoes+= "-" +Calendar.TUESDAY;
                }
			    if (qua.isChecked()){
                    repeticoes+= "-" +Calendar.WEDNESDAY;
                }
			    if (qui.isChecked()){
                    repeticoes+= "-" +Calendar.THURSDAY;
                }
			    if (sex.isChecked()){
                    repeticoes+= "-" + Calendar.FRIDAY;
                }
			    if (sab.isChecked()){
                    repeticoes+= "-" +Calendar.SATURDAY;
                }
			  
                
				if (repeticoes.isEmpty()){
					AlertDialog.Builder builder = new AlertDialog.Builder(EditClassView.this);
					builder.setMessage("Please, choose one or more days for repeat")
					       .setCancelable(false)
					     
					       .setNeutralButton("Ok", new DialogInterface.OnClickListener() {
					           public void onClick(DialogInterface dialog, int id) {
					                dialog.cancel();
					           }
					       });
					builder.show();
				}else {
					
				    repeticoes = repeticoes.substring(1);
					int hour = timePicker.getCurrentHour();
					int minute = timePicker.getCurrentMinute();
					String initialTime = hour + ":" + minute;
					int hour2 = timePicker2.getCurrentHour();
                    int minute2 = timePicker2.getCurrentMinute();
                    String endTime = hour2 + ":" + minute2;

					editBD(repeticoes, initialTime, endTime);
					AlertDialog.Builder builder = new AlertDialog.Builder(EditClassView.this);
					builder.setMessage("Classroom save")
						       .setCancelable(false)	
					  		
						       .setNeutralButton("Ok", new DialogInterface.OnClickListener() {
						           public void onClick(DialogInterface dialog, int id) {
						                dialog.cancel();
						                back();
						           }
						       });
					builder.show();
			
				}
				
			}
		});
	    
	}
	
	
	protected void editBD(String repeticoes, String initialTime, String endTime) {
		BD bd = new BD();
		System.out.println(id +" "+ initialDate +" "+ endDate +" "+ initialTime +" "+ endTime +" "+  repeticoes +" "+  subjectName);
		bd.editClass(this, String.valueOf(id), initialDate, endDate, initialTime, endTime, repeticoes, subjectName);
		
	}

	public void onNewIntent(Intent intent) {
		String dateText = intent.getStringExtra("date");
		String[] dateArr = dateText.split("-"); // date format is yyyy-mm-dd
		date.set(Integer.parseInt(dateArr[0]), Integer.parseInt(dateArr[1]), Integer.parseInt(dateArr[2]));
	}
	
	@Override
	public void onBackPressed() {
        back();	        		 
        return;
	}

	private void back() {
		int month = date.get(Calendar.MONTH);
        int year = date.get(Calendar.YEAR);
        
	    Intent trocatela = new Intent(EditClassView.this, SubjectsView.class);
		trocatela.putExtra("date", year+"-"+month+"-"+today);
		trocatela.putExtra("subjectName", subjectName);
		trocatela.putExtra("subjectTeacher", subjectTeacher);
		trocatela.putExtra("subjectPlace", subjectPlace);
		trocatela.putExtra("credits", String.valueOf(subjectCredits));
		trocatela.putExtra("semesterName", getIntent().getStringExtra("semesterName"));
        trocatela.putExtra("initialDate", getIntent().getStringExtra("initialDate"));
        trocatela.putExtra("endDate", getIntent().getStringExtra("endDate"));
        EditClassView.this.startActivity(trocatela);
        EditClassView.this.finish();
	}
}
